﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Api.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Api.Common;
using Api.Model.Dto.ArticleDto;
using Api.IServices;
using AutoMapper;
using Api.Model.Model;
using System.IO;
using Api.StartUpConfig;
using Microsoft.AspNetCore.Authorization;

namespace Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ArticleController : ApiBaseController
    {
        private readonly IArticleServices _articleServices;

        public ArticleController(IArticleServices articleServices)
        {
            _articleServices = articleServices;
        }
        [HttpPost]
        public async Task CreateArticleAsync(CreateArticleInput input)
        {
            await _articleServices.CreateArticleAsync(input);
        }
        [HttpGet]
        public async Task<List<GetArticleListOutput>> GetArticleListAsync([FromQuery] GetArticleInput input)
        {
            return await _articleServices.GetArticlePageListAsync(input);
        }
        [HttpGet]
        public async Task<GetArticleOutput> GetArticleAsync([FromQuery] int? id)
        {
            return await _articleServices.GetArticleAsync(id);
        }
        [HttpPost]
        public async Task<object> UpLoadArticleImageAsync([FromForm]IFormFile image)
        {
            if (image == null)
            {
                throw new UIException("图片不能为空!");
            }
            var fileFormat = image.FileName.Substring(image.FileName.LastIndexOf(".") + 1);
            ReuseMethod.ImageFormatCheck(image.FileName);
            var buffer = await  ReuseMethod.GetFileBufferAsync(image);
            var dir = StaticInfo.WebRootPath + "/ArticleImage/";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var path = dir + Guid.NewGuid() + "." + fileFormat;
            await System.IO.File.WriteAllBytesAsync(path, buffer);
            return path.Substring(path.IndexOf("/ArticleImage/"));
        }
        [HttpDelete]
        public void DeleteArticleImage(string imageName)
        {
            if (string.IsNullOrWhiteSpace(imageName))
            {
                throw new UIException("图片不能为空!");
            }
            ReuseMethod.ImageFormatCheck(imageName);
            var path = StaticInfo.WebRootPath  + imageName.Substring(imageName.LastIndexOf("/ArticleImage/"));
            if (!System.IO.File.Exists(path))
            {
                throw new UIException("图片不存在!");
            }
            System.IO.File.Delete(path);
        }
        [HttpPut]
        public async Task UpdateArticle(UpdateArticleInput input)
        {
            await _articleServices.UpdateArticle(input);
            
        }
        [HttpDelete]
        public async Task DeleteArticle(int? id)
        {
            await _articleServices.DeleteArticle(id.GetValueOrDefault());
        }
    }
}